﻿using CubicFWNet.Data.SystemManager;
/*******************************************************************************
 * Copyright © 2016 NFine.Framework 版权所有
 * Author: NFine
 * Description: NFine快速开发平台
 * Website：http://www.nfine.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using System.Linq;
using System.Threading.Tasks;

namespace CubicFWNet.Application.SystemManager
{
    public class LogApp
    {
        private LogInLogRepository service = new LogInLogRepository();

        public IEnumerable<LogInLog> Get(string keyword, string timeType, int pageIndex, int pageSize)
        {
            var expression = ExtLinq.True<LogInLog>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FamilyName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(timeType))
            {
                DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
                DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
                switch (timeType)
                {
                    case "1":
                        break;
                    case "2":
                        startTime = DateTime.Now.AddDays(-7);
                        break;
                    case "3":
                        startTime = DateTime.Now.AddMonths(-1);
                        break;
                    case "4":
                        startTime = DateTime.Now.AddMonths(-3);
                        break;
                    default:
                        break;
                }
                expression = expression.And(t => t.Time >= startTime && t.Time <= endTime);
            }
            return service.IQueryable(expression).OrderByDescending(a=>a.Time).Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
        public void RemoveLog(string keepTime)
        {
            DateTime operateTime = DateTime.Now;
            if (keepTime == "7")            //保留近一周
            {
                operateTime = DateTime.Now.AddDays(-7);
            }
            else if (keepTime == "1")       //保留近一个月
            {
                operateTime = DateTime.Now.AddMonths(-1);
            }
            else if (keepTime == "3")       //保留近三个月
            {
                operateTime = DateTime.Now.AddMonths(-3);
            }
            var expression = ExtLinq.True<LogInLog>();
            expression = expression.And(t => t.Time <= operateTime);
            service.Delete(expression);
        }

        /// <summary>
        /// 写日志 同步方法
        /// </summary>
        /// <param name="LogInLog"></param>
        public void WriteDbLog(LogInLog LogInLog)
        {
            LogInLog.Id = Guid.NewGuid();
            LogInLog.Time = DateTime.Now;
            LogInLog.IPAddress = Net.Ip;
            LogInLog.IPAddressName = Net.GetLocation(LogInLog.IPAddress);
            service.Insert(LogInLog);
        }

        /// <summary>
        /// 写日志 异步方法
        /// </summary>
        /// <param name="LogInLog"></param>
        public Task WriteDbLogAsync(LogInLog LogInLog)
        {
           return Task.Run(() => {
                LogInLog.Id = Guid.NewGuid();
                LogInLog.Time = DateTime.Now;
                LogInLog.IPAddress = Net.Ip;
                LogInLog.IPAddressName = Net.GetLocation(LogInLog.IPAddress);
                service.Insert(LogInLog);
            });            
        }
    }
}
